#include "util.hpp"
#include "data.hpp"
extern tqbcloud::DataManager _Data;
namespace tqbcloud
{
    class HotManager
    {
    private:
        std::string _back_dir;
        std::string _pack_dir;
        std::string _arc_suffix;
        std::string _url_prefix;
        time_t _hot_time;

    public:
        HotManager()
        {
            Config *c = Config::getinstance();
            _back_dir = c->GetBackDir();
            _pack_dir = c->GetPackDir();
            _arc_suffix = c->GetArcSuffix();
            _url_prefix = c->GetURLPrefix();
            _hot_time = c->Gethottime();
            tqbcloud::Fileutil f1(_back_dir);
            tqbcloud::Fileutil f2(_pack_dir);
            f1.CreateDirectory();
            f2.CreateDirectory();
        }
        bool HotJudge(const std::string &file)
        { // 热点文件返回false 非热点文件返回true
            tqbcloud::Fileutil tmp(file);
            time_t lasttime = tmp.Lastacctime();
            time_t curtime = time(NULL);
            if ((curtime - lasttime) > _hot_time)
            {
                return true;
            }
            return false;
        }
        bool RunModule()
        {
            while (1)
            {
                tqbcloud::Fileutil fu(_back_dir);
                std::vector<std::string> arry;
                fu.ScanDirectory(&arry);
                for (auto &a : arry)
                {
                    if (HotJudge(a) == false)
                    {
                        continue;
                    }
                    tqbcloud::BackupInfo fo;
                    if (_Data.GetOneByRealPath(a, &fo) == false)
                    {
                        fo.NewBackupInfo(a);
                    }
                    tqbcloud::Fileutil tmp(fo._realpath);
                    tmp.Compress(fo._packpath);
                    fu.Remove(a);
                    fo._pack_flag = true;
                    _Data.Update(fo._url,fo);
                }
                usleep(100);//防止空文件夹循环过快占用cpu资源
            }
        }
    };

}